package org.zgy.stock.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zgy.stock.dto.StockBaseDto;
import org.zgy.stock.mapper.StockBaseMapper;
import org.zgy.stock.model.StockBase;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class StockBaseService {
    @Autowired
    private StockBaseMapper stockBaseMapper;


    public void insertStockBase(List<StockBaseDto> stockBaseDtoList) {
        Set<StockBase> stockBaseList = new HashSet<>();
        Set<String> nameList = new HashSet<>();
        stockBaseDtoList.parallelStream().forEach(stockBaseDto -> {
            StockBase stockBase = new StockBase();
            stockBase.setCode(stockBaseDto.getStockCode());
            stockBase.setName(stockBaseDto.getStockName());
            if (!nameList.contains(stockBaseDto.getStockName())) {
                nameList.add(stockBaseDto.getStockName());
                synchronized (stockBaseList) {
                    stockBaseList.add(stockBase);
                    if (stockBaseList.size() > 100) {
                        stockBaseMapper.insert(stockBaseList);
                        stockBaseList.clear();
                    }
                }

            }
        });
        stockBaseMapper.insert(stockBaseList);
        stockBaseList.clear();
        stockBaseDtoList.clear();
    }

    public List<String> getList() {
        var list = stockBaseMapper.getStockCodeList();
        return list;
    }
}
